package com.website.dayouziblog.job;

import com.website.dayouziblog.mapper.dayouziblog.ArticleMapper;
import com.website.dayouziblog.mapper.informationSchema.ArticleTotalsMapper;
import com.website.dayouziblog.utils.RedisUtil;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Map;

/**
 * 统计文章总数定时任务
 */
@Component
public class ArticleTotalsJob extends QuartzJobBean {


    @Resource
    private ArticleTotalsMapper articleTotalsMapper;

    @Autowired
    private RedisUtil redisUtil;


    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        String key = jobDataMap.get("key").toString();
        //获取文章总数
        Map map = articleTotalsMapper.getArticleTotals(key);
        String total = map.get("total").toString();
        int num = Integer.parseInt(total);
        //存入redis
        redisUtil.set("Article:totals", num, 180);
    }
}
